using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using MediaOwl.Common.ScreenManagement;

namespace MediaOwl.Common.ViewModels
{
    [Export, PartCreationPolicy(CreationPolicy.NonShared)]
    public class ShowPictureSingleViewModel : Workitem<IWorkspace>
    {
        #region Fields
        private BitmapSource picture;
        #endregion

        #region Constructor

        public ShowPictureSingleViewModel()
        {
            ScrollBarVisibility = ScrollBarVisibility.Disabled;
            Image = new Image();
            Image.ImageOpened += (s, e) => Run.Coroutine(Show.NotBusy(this));

            Image.ImageFailed += (s, e) =>
                                     {
                                         Run.Coroutine(Show.NotBusy(this));
                                         Run.Coroutine(Show.Dialog<DialogMessageViewModel>()
                                             .In(this)
                                             .Configured(x => x.WithData(
                                                 string.Format("Could not open the picture {0}; {1}", e.OriginalSource, e.ErrorException.Message))));
                                     };
        }

        #endregion

        #region Properties & Backingfields

        private bool isOriginalSize;
        public bool IsOriginalSize
        {
            get { return isOriginalSize; }
            set
            {
                isOriginalSize = value;
                NotifyOfPropertyChange(() => IsOriginalSize);
                ChangeSize();
            }
        }

        private Image image;
        public Image Image
        {
            get { return image; }
            set
            {
                image = value;
                NotifyOfPropertyChange(() => Image);
            }
        }

        private ScrollBarVisibility scrollBarVisibility;
        public ScrollBarVisibility ScrollBarVisibility
        {
            get { return scrollBarVisibility; }
            set { scrollBarVisibility = value; NotifyOfPropertyChange(() => ScrollBarVisibility); }
        }

        #endregion

        #region Methods
        public void WithPicture(BitmapSource bitmapImage, string title)
        {
            picture = bitmapImage;
            DisplayName = string.Format(title);
            Run.Coroutine(LoadImage());
        }

        public void WithPicture(Uri imageUri, string title)
        {
            picture = new BitmapImage(imageUri);
            DisplayName = string.Format(title);
            Run.Coroutine(LoadImage());
        }

        public IEnumerable<IResult> LoadImage()
        {
            yield return Show.Busy(this);
            Image.Source = picture;
            //NotBusy is called on Image.ImageOpened
        }

        public void ChangeSize()
        {
            if (IsOriginalSize)
            {
                Image.Height = picture.PixelHeight;
                Image.Width = picture.PixelWidth;
                Image.HorizontalAlignment = HorizontalAlignment.Center;
                Image.VerticalAlignment = VerticalAlignment.Center;
                ScrollBarVisibility = ScrollBarVisibility.Auto;
            }
            else
            {
                Image.Height = double.NaN;
                Image.Width = double.NaN;
                Image.HorizontalAlignment = HorizontalAlignment.Stretch;
                Image.VerticalAlignment = VerticalAlignment.Stretch;
                ScrollBarVisibility = ScrollBarVisibility.Disabled;
            }
        }
        #endregion
    }
}